const { ContinuousLogger } = require('./log')
const util = require('util')

function uploadFile() {
    const logger = ContinuousLogger.create()
    let serverFile, clientFile, msg, isSuccess
    return {
        onProgress(progress) {
            switch (progress.status) {
                case this.Status.STARTED:
                    logger.inf('开始上传...%s', progress.clientFile)
                    clientFile = progress.clientFile
                    serverFile = progress.serverFile
                    break
                case this.Status.IN_PROGRESS:
                    logger.inf('正在上传...%s', progress.percentage + '%')
                    break
                case this.Status.FAILURE:
                    logger.err(
                        '上传文件失败。文件: %s, 错误: %s',
                        progress.clientFile,
                        progress.error.message
                    )
                    msg = util.format(
                        '上传文件失败。\n本地: %s\n远程: %s\n错误: %s',
                        clientFile,
                        serverFile,
                        progress.error.message || '未知错误'
                    )
                    isSuccess = false
                    break
                case this.Status.SUCCESS:
                    logger.inf('上传文件成功, 已上传 %s。', progress.serverFile)
                    msg = util.format(
                        '上传文件成功。\n本地: %s\n远程: %s\n',
                        clientFile,
                        serverFile
                    )
                    isSuccess = true
                    break
            }
        },
        async showDetail(title = '', btns = ['打开', '详情']) {
            try {
                if (title) {
                    title = title + '\n'
                }
                let btn
                if (isSuccess) {
                    btn = await logger.showDetail(title + msg, btns)
                } else {
                    btn = await logger.showError(title + msg, btns)
                }
                if (btn == '打开') {
                    import('open').then(open => open.default(clientFile))
                }
                return btn
            } catch (error) {
                hx.window.showErrorMessage(error.message)
            }
        },
        logger
    }
}

function downloadFile() {
    const logger = ContinuousLogger.create()
    let serverFile, clientFile, msg, isSuccess
    return {
        onProgress(progress) {
            switch (progress.status) {
                case this.Status.STARTED:
                    logger.inf('开始下载...%s', progress.serverFile)
                    clientFile = progress.clientFile
                    serverFile = progress.serverFile
                    break
                case this.Status.IN_PROGRESS:
                    logger.inf('正在下载...%s', progress.percentage + '%')
                    break
                case this.Status.FAILURE:
                    logger.err(
                        '下载文件失败。文件: %s, 错误: %s',
                        progress.clientFile,
                        progress.error.message
                    )
                    msg = util.format(
                        '下载文件失败。\n本地: %s\n远程: %s\n错误: %s',
                        clientFile,
                        serverFile,
                        progress.error.message || '未知错误'
                    )
                    isSuccess = false
                    break
                case this.Status.SUCCESS:
                    logger.inf('下载文件成功, 已下载 %s。', progress.serverFile)
                    msg = util.format(
                        '下载文件成功。\n本地: %s\n远程: %s\n',
                        clientFile,
                        serverFile
                    )
                    isSuccess = true
                    break
            }
        },
        async showDetail(title = '', btns = ['打开', '详情']) {
            if (title) {
                title = title + '\n'
            }
            let btn
            if (isSuccess) {
                btn = await logger.showDetail(title + msg, btns)
            } else {
                btn = await logger.showError(title + msg, btns)
            }
            if (btn == '打开') {
                import('open').then(open => open.default(clientFile))
            }
            return btn
        },
        logger
    }
}

function uploadPath() {
    const logger = ContinuousLogger.create()
    let clientPath, serverPath, msg, isSuccess
    return {
        onProgress(progress) {
            switch (progress.status) {
                case this.Status.STARTED:
                    clientPath = progress.clientPath
                    serverPath = progress.serverPath
                    logger.inf('开始上传...%s', progress.clientPath)
                    break
                case this.Status.IN_PROGRESS:
                    logger.inf('上传文件...%s', progress.serverFile)
                    break
                case this.Status.PARTIAL_FAILURE:
                    {
                        logger.err('上传完成，但有部分失败。')
                        const successCount = progress.successs.length
                        const failureCount = progress.failures.length
                        msg = util.format(
                            '部分失败。\n本地: %s\n远程: %s\n成功: %s, 失败: %s',
                            clientPath,
                            serverPath,
                            successCount,
                            failureCount
                        )
                        isSuccess = false
                    }
                    break
                case this.Status.FAILURE:
                    {
                        logger.err('上传失败。')
                        const successCount = 0
                        const failureCount = progress.failures.length
                        msg = util.format(
                            '上传失败。\n本地: %s\n远程: %s\n成功: %s, 失败: %s\n错误: %s',
                            clientPath,
                            serverPath,
                            successCount,
                            failureCount,
                            progress.error?.message || '未知异常'
                        )
                        isSuccess = false
                    }
                    break
                case this.Status.SUCCESS:
                    {
                        logger.inf('上传文件夹成功, 已上传 %s。', progress.serverPath)
                        const successCount = progress.successs.length
                        const failureCount = 0
                        msg = util.format(
                            '上传成功。\n本地: %s\n远程: %s\n成功: %s, 失败: %s',
                            clientPath,
                            serverPath,
                            successCount,
                            failureCount
                        )
                        isSuccess = true
                    }
                    break
            }
        },
        async showDetail(title = '', btns = ['打开', '详情']) {
            if (title) {
                title = title + '\n'
            }
            let btn
            if (isSuccess) {
                btn = await logger.showDetail(title + msg, btns)
            } else {
                btn = await logger.showError(title + msg, btns)
            }
            if (btn == '打开') {
                import('open').then(open => open.default(clientPath))
            }
            return btn
        },
        logger
    }
}

function downloadPath() {
    const logger = ContinuousLogger.create()
    let clientPath, serverPath, msg, isSuccess
    return {
        onProgress(progress) {
            switch (progress.status) {
                case this.Status.STARTED:
                    clientPath = progress.clientPath
                    serverPath = progress.serverPath
                    logger.inf('开始下载...%s', progress.clientPath)
                    break
                case this.Status.IN_PROGRESS:
                    logger.inf('下载文件...%s', progress.serverFile)
                    break
                case this.Status.PARTIAL_FAILURE:
                    {
                        logger.err('下载完成，但有部分失败。')
                        const successCount = progress.successs.length
                        const failureCount = progress.failures.length
                        msg = util.format(
                            '下载结束, 有部分下载失败。\n本地: %s\n远程: %s\n成功: %s, 失败: %s\n错误: %s',
                            clientPath,
                            serverPath,
                            successCount,
                            failureCount,
                            progress.error?.message || '未知异常'
                        )
                        isSuccess = false
                    }
                    break
                case this.Status.FAILURE:
                    {
                        logger.err('上传失败。')
                        const successCount = 0
                        const failureCount = progress.failures.length
                        msg = util.format(
                            '下载失败。\n本地: %s\n远程: %s\n成功: %s, 失败: %s',
                            clientPath,
                            serverPath,
                            successCount,
                            failureCount
                        )
                        isSuccess = false
                    }
                    break
                case this.Status.SUCCESS:
                    {
                        logger.inf('上传文件夹成功, 已上传 %s。', progress.serverPath)
                        const successCount = progress.successs.length
                        const failureCount = 0
                        msg = util.format(
                            '下载成功。\n本地: %s\n远程: %s\n成功: %s, 失败: %s',
                            clientPath,
                            serverPath,
                            successCount,
                            failureCount
                        )
                        isSuccess = true
                    }
                    break
            }
        },
        async showDetail(title = '', btns = ['打开', '详情']) {
            if (title) {
                title = title + '\n'
            }
            let btn
            if (isSuccess) {
                btn = await logger.showDetail(title + msg, btns)
            } else {
                btn = await logger.showError(title + msg, btns)
            }
            if (btn == '打开') {
                import('open').then(open => open.default(clientPath))
            }
            return btn
        },
        logger
    }
}

module.exports = {
    Upload: {
        File: uploadFile,
        Path: uploadPath
    },
    Download: {
        File: downloadFile,
        Path: downloadPath
    }
}
